package cn.digitalize.common.pojo.result;

import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;


/**
 * @author Created by ZhangCheng on 2023/06/01 (返回结果封装)
 */
@Setter
@Getter
public class ResultData<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 成功
     */
    public static final int SUCCESS = 200;

    /**
     * 失败
     */
    public static final int ERROR = 500;

    /**
     * 状态码
     */
    private Integer code;

    /**
     * 返回信息
     */
    private String msg;

    /**
     * 返回数据
     */
    private T data;

    /**
     * 查询数据返回
     *
     * @param data
     * @return
     */
    public static <T> ResultData<T> query(T data) {
        ResultData<T> resultData = new ResultData<T>();
        resultData.setCode(ResultData.SUCCESS);
        resultData.setData(data);
        return resultData;
    }

    /**
     * 返回成功
     *
     * @param <T>
     * @return
     */
    public static <T> ResultData<T> success() {
        ResultData<T> resultData = new ResultData<T>();
        resultData.setCode(ResultData.SUCCESS);
        return resultData;
    }

    /**
     * 返回成功信息
     *
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> ResultData<T> success(String msg) {
        ResultData<T> resultData = new ResultData<T>();
        resultData.setCode(ResultData.SUCCESS);
        resultData.setMsg(msg);
        return resultData;
    }

    /**
     * 返回错误信息
     *
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> ResultData<T> error(String msg) {
        ResultData<T> resultData = new ResultData<T>();
        resultData.setCode(ResultData.ERROR);
        resultData.setMsg(msg);
        return resultData;
    }

    /**
     * 返回操作成功
     *
     * @param <T>
     * @return
     */
    public static <T> ResultData<T> handleSuccess() {
        ResultData<T> resultData = new ResultData<T>();
        resultData.setCode(ResultData.SUCCESS);
        resultData.setMsg("操作成功！");
        return resultData;
    }

    /**
     * 返回删除成功
     *
     * @param <T>
     * @return
     */
    public static <T> ResultData<T> deleteSuccess() {
        ResultData<T> resultData = new ResultData<T>();
        resultData.setCode(ResultData.SUCCESS);
        resultData.setMsg("删除成功！");
        return resultData;
    }
}
